const App = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		// 售后单id
		order_refund_id: null,

		// 订单商品详情
		detail: {},

		// 物流公司索引
		expressIndex: -1,
		reason: 0,
		image: ['/images/icon/morentupain.png'],
		imageid: []
	},

	disable: false,

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

	},
	bindreason() {
		this.setData({
			reason: this.data.reason == 0 ? 1 : 0
		});
	},
	loadImg() {
		var that = this;
		wx.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success: function(res) {
				var tempFilePaths = res.tempFilePaths;
				App._uplodeimg(tempFilePaths[0], 'upload/image', result => {
					let res = JSON.parse(result.data)
					let imgs = that.data.image;
					
					that.data.imageid.push(res.data.file_id)
					console.log(that.data.imageid)
					if (imgs.length == 1) {
						imgs.unshift(res.data.file_path)
					} else {
						imgs.pop()
						imgs.push(res.data.file_path)
						imgs.push('/images/icon/morentupain.png')
					}
					if (imgs.length == 6) {
						imgs.pop()
					}
					that.setData({
						image: imgs,
						imageid:that.data.imageid
					});
				})
			}
		})
	
	},
	/**
	 * 获取售后单详情
	 */
	getRefundDetail: function() {
		let _this = this;
		App._get('user.refund/detail', {
			order_refund_id: this.data.order_refund_id
		}, function(result) {
			_this.setData(result.data);
		});
	},

	/**
	 * 跳转商品详情
	 */
	onGoodsDetail: function(e) {
		// 记录formId
		App.saveFormId(e.detail.formId);
		wx.navigateTo({
			url: '../../../goods/index?goods_id=' + e.detail.target.dataset.id
		});
	},

	/**
	 * 凭证图片预览
	 */
	previewImages: function(e) {
		let imageUrls = [];
		this.data.detail.image.forEach(function(item) {
			imageUrls.push(item.file_path);
		});
		wx.previewImage({
			current: imageUrls[e.target.dataset.index],
			urls: imageUrls
		})
	},

	/**
	 * 选择物流公司 picker
	 */
	onExpressChange: function(e) {
		this.setData({
			expressIndex: e.detail.value
		})
	},

	/**
	 * 表单提交
	 */
	onSubmit: function(e) {
		let _this = this,
			values = e.detail.value;

		// 记录formId
		App.saveFormId(e.detail.formId);

		// 判断是否重复提交
		if (_this.disable === true) {
			return false;
		}

		// 表单提交按钮设为禁用 (防止重复提交)
		_this.disable = true;

		wx.showLoading({
			title: '正在处理...',
			mask: true
		});

		// 提交到后端
		values['order_refund_id'] = _this.data.order_refund_id;
		App._post_form('user.refund/delivery', values, function(result) {
			App.showSuccess(result.msg, function() {
				// 获取售后单详情
				_this.getRefundDetail();
			});
		}, false, function() {
			wx.hideLoading();
			// 解除禁用
			_this.disable = false;
		});

	},

})
